package redis

import (
	"context"

	"gitee.com/youkelike/ziam/zredis"
	"gitee.com/youkelike/zlog"
	"github.com/mitchellh/mapstructure"
)

const (
	RedisKeyPrefix      = "analytics-"
	defaultRedisAddress = "127.0.0.1:6379"
)

// redis 作为底层存储的仓库 store
type RedisClusterStorageManager struct {
	// KeyPrefix string
	HashKeys bool
	Config   zredis.Config
	client   zredis.RedisCluster
}

func (r *RedisClusterStorageManager) GetName() string {
	return "redis"
}

func (r *RedisClusterStorageManager) Init(config interface{}) error {
	r.Config = zredis.Config{}
	err := mapstructure.Decode(config, &r.Config)
	if err != nil {
		zlog.Fatalw("failed to decode configuration: %s", err.Error())
	}

	// 先启动连接池循环检测
	zredis.ConnectToRedis(context.Background(), &r.Config)
	// 构造客户端
	r.client = zredis.RedisCluster{
		KeyPrefix: RedisKeyPrefix,
	}

	return nil
}

func (r *RedisClusterStorageManager) GetAndDeleteSet(ctx context.Context, keyName string) []interface{} {
	return r.client.GetAndDeleteSet(ctx, keyName)
}
